﻿<UserControl x:Class="ApplicationUI.FileSystemView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ApplicationUI="clr-namespace:ApplicationUI"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    xmlns:Controls="clr-namespace:WtfManager.Controls;assembly=WtfManager.Controls"
    xmlns:Commands="clr-namespace:WtfManager.UIManager.Commands;assembly=WtfManager.UIManager"
    xmlns:AvalonDock="clr-namespace:AvalonDock;assembly=AvalonDock"
    xmlns:FileManager="clr-namespace:WtfManager.FileManager;assembly=WtfManager.FileManager"
    xmlns:UIManager="clr-namespace:WtfManager.UIManager;assembly=WtfManager.UIManager"
    xmlns:HierarchicalData="clr-namespace:WtfManager.HierarchicalData;assembly=WtfManager.HierarchicalDataManager"
    xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    Height="Auto" Width="Auto" Name="fileSystemView">
  <UserControl.Resources>

    <System:Boolean x:Key="true">true</System:Boolean>

    <ItemsPanelTemplate x:Key="tileViewItemsPanelTemplate">
      <WrapPanel
          Width="{Binding (FrameworkElement.ActualWidth),RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
          ItemWidth="70" ItemHeight="70" Margin="0,0,0,0" />
    </ItemsPanelTemplate>

    <DataTemplate x:Key="tileViewItemTemplate">
      <Grid Width="Auto" Focusable="True">
        <Grid.RowDefinitions>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="67" MaxWidth="67"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Image Grid.Row="0" Grid.Column="0" Width="30" Height="30"
            Source="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=DataContext, 
            Converter={x:Static HierarchicalData:TreeItemToImageConverter.Instance}, ConverterParameter={StaticResource true}}" Margin="0,0,0,0" />
        <TextBlock TextTrimming="CharacterEllipsis" TextAlignment="Center"
            TextWrapping="Wrap" MaxWidth="67" Width="Auto" Grid.Row="1" Grid.Column="0"
            Text="{Binding Path=Name}" FontSize="10" HorizontalAlignment="Center"
            Margin="0,0,0,0" Height="50" />
      </Grid>
    </DataTemplate>

    <Controls:GenericView x:Key="tileView"
        ItemTemplate="{StaticResource tileViewItemTemplate}"
        ItemsPanel="{StaticResource tileViewItemsPanelTemplate}" />
    
    <UIManager:CollectionViewSettings x:Key="viewSettings">
                                      
      <UIManager:CollectionViewSettings.SortDescriptionCollection>
        
        <ComponentModel:SortDescription PropertyName="IsFile" Direction="Ascending" />
        <ComponentModel:SortDescription PropertyName="Name" Direction="Ascending" />
        
      </UIManager:CollectionViewSettings.SortDescriptionCollection>
      
      <UIManager:CollectionViewSettings.Filters>
        
        <FileManager:GenericFileSystemItemFilter PropertyName="IsHidden">
          <FileManager:GenericFileSystemItemFilter.Value>
            <System:Boolean>false</System:Boolean>
          </FileManager:GenericFileSystemItemFilter.Value>
        </FileManager:GenericFileSystemItemFilter>
        
      </UIManager:CollectionViewSettings.Filters>
      
    </UIManager:CollectionViewSettings>
  </UserControl.Resources>
  
  <Grid Width="Auto" Height="Auto">

    <Grid.RowDefinitions>
      <RowDefinition Height="20" />
      <RowDefinition />
    </Grid.RowDefinitions>

    <TextBox Name="_path" Grid.Row="0" Height="20" Width="Auto" AcceptsReturn="False" TabIndex="1"
        Text="{Binding Converter={x:Static FileManager:FileSystemItemToPathConverter.Instance}, Mode=OneWay}">
      <TextBox.CommandBindings>
        <Commands:WtfCommandBinding Command="ApplicationCommands.Open" />
      </TextBox.CommandBindings>
      <TextBox.InputBindings>
        <KeyBinding Command="ApplicationCommands.Open" Key="Enter">
          <KeyBinding.CommandParameter>
            <Commands:OpenCommandParameters
                ItemsToOpen="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=CommandTarget.Text, 
                Converter={x:Static FileManager:PathToFileSystemItemConverter.Instance}}" />
          </KeyBinding.CommandParameter>
        </KeyBinding>
      </TextBox.InputBindings>
    </TextBox>

    <ListView x:Name="_list" Grid.Row="1" Height="Auto" Width="Auto" TabIndex="0" AllowDrop="True"
        ItemsSource="{Binding Path=Children, Converter={x:Static UIManager:ItemsSourceToCollectionViewSourceConverter.Instance}, ConverterParameter={StaticResource viewSettings}}" 
        View="{StaticResource tileView}"
        Controls:WtfDocumentContent.IsDefaultFocusedElement="True"
        UIManager:DragDropManager.AllowDrag="True"
        UIManager:DragDropManager.AllowDrop="True">
      
      <ListView.CommandBindings>
        <Commands:WtfCommandBinding Command="ApplicationCommands.Open" />
        <Commands:WtfCommandBinding Command="Commands:WtfNavigationCommands.NavigateUp" />
        <Commands:WtfCommandBinding Command="Commands:WtfNavigationCommands.OpenInNewTab" />
        <Commands:WtfCommandBinding Command="FileManager:FileSystem.ShowShellContextMenuCommand" />
      </ListView.CommandBindings>
      
      <ListView.InputBindings>
        <KeyBinding Command="ApplicationCommands.Open" Key="Enter" />
        <KeyBinding Command="Commands:WtfNavigationCommands.NavigateUp" Key="Back" />
        <KeyBinding Command="FileManager:FileSystem.ShowShellContextMenuCommand" Key="Apps" Modifiers="Shift" />
      </ListView.InputBindings>
      
      <UIManager:SelectionManager.SelectionManager>
        <HierarchicalData:TreeItemSelectionManager />
      </UIManager:SelectionManager.SelectionManager>
      
      <ListView.ItemContainerStyle>
        <Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="{x:Type ListBoxItem}">
          <Setter Property="Commands:CommandsService.CommandBindings">
            <Setter.Value>
              <Commands:CommandBindingsCollection>
                <Commands:WtfCommandBinding Command="ApplicationCommands.Open">                
                </Commands:WtfCommandBinding>
              </Commands:CommandBindingsCollection>
            </Setter.Value>
          </Setter>
          <Setter Property="Commands:CommandsService.InputBindings">
            <Setter.Value>
              <Commands:InputBindingsCollection>
                <MouseBinding Command="ApplicationCommands.Open"
                    MouseAction="LeftDoubleClick" />
              </Commands:InputBindingsCollection>
            </Setter.Value>
          </Setter>
          
          <Setter Property="UIManager:DragDropManager.IsDragable" Value="true" />
          <Setter Property="UIManager:DragDropManager.AllowDrop" Value="true" />
        </Style>
      </ListView.ItemContainerStyle>

      <ListView.ContextMenu>
        <ContextMenu UIManager:MenuManager.ProvideMenuItems="True"
            UIManager:MenuManager.SettingsFileName="FileSystemViewContextMenu.xml" />
      </ListView.ContextMenu>

    </ListView>
  </Grid>
  
</UserControl>
